package edu.yngsxy.wkstudy;

import edu.yngsxy.wkstudy.core.config.WxConfig;
import edu.yngsxy.wkstudy.core.exception.BusinessException;
import edu.yngsxy.wkstudy.core.model.common.Result;
import edu.yngsxy.wkstudy.test.TestUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "测试", description = "用于知识测试") // 分组标签
public class TestController {
    @RequestMapping("/hello")
    public String hello() {
        return "您好，Spring Boot！！！!";
    }

    @Autowired
    private Environment env;

    @RequestMapping("/hello2")
    public String hello2() {
        return "您好，" + env.getProperty("my.testmsg") + "!";
    }

    @Value("${my.testmsg}")
    private String testmsg;

    @RequestMapping("/hello3")
    public String hello3() {
        return "您好，" + testmsg + "!";
    }

    @Autowired
    WxConfig wxConfig;

    @RequestMapping("/hello4")
    public String hello4() {
        return wxConfig.toString();
    }


    private Log log = LogFactory.getLog(TestController.class);
    @Operation(summary = "测试5", description = "测试日志功能")
    @GetMapping("/hello5")
    public String hello5() {
        log.info("测试日志,普通消息");
        return wxConfig.toString();

    }

        /*省略其他方法*/
        @GetMapping("/hello6")
        //@Tag(name = "测试", description = "描述") // 分组标签，在方法上添加试试查看什么效果？
        @Operation(summary = "测试5", description = "测试日志功能")
        @ApiResponses({
                @ApiResponse(responseCode = "200", description = "访问成功"),
                @ApiResponse(responseCode = "404", description = "资源不存在")
        })
        public String hello6() {
            log.warn("apiw文档测试");
            return wxConfig.toString();
        }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询用户", description = "传入用户ID，返回用户详情")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public TestUser getUserById(
            @Parameter(description = "用户ID", required = true, example = "1001")
            @PathVariable Long id) {
        // 业务逻辑...
        return new TestUser(4L,"张三", 20);
    }
    @PostMapping("/add")
    @Operation(summary = "创建用户", description = "传入用户信息，创建新用户")
    public TestUser createUser(
            @Parameter(description = "用户信息", required = true)
            @RequestBody TestUser user) {
        // 业务逻辑...
        return user;
    }

    @Operation(summary = "测试7", description = "统一返回结果对象测试")
    @GetMapping("/hello7")
    public Result<Object> hello7() {
        log.info("测试日志,普通消息");
        return Result.success("登录成功","账号密码123456");

    }


    @Operation(summary = "测试8", description = "统一返回结果对象测试")
    @GetMapping("/hello8")
    public String hello8() {
        log.info("测试日志,普通消息");
        return ("登录成功");
        //插入GlobalResponseHandler
        //GlobalResponseHandler .supports()
        //GlobalResponseHandler .beforeBodyWrite

    }
    @Operation(summary = "测试9", description = "测试异常处理")
    @GetMapping("/hello9")
    public String hello9() {
        throw new BusinessException("订单不存在");
    }
}